package kotlinetc

import leetcode.ListNode

fun Any.println() = println(this)


fun ListNode?.toList(): ArrayList<Int> = kotlin.run {
    val list = arrayListOf<Int>()

    var p :ListNode?= this

    while (p != null) {
        list.add(p.`val`)
        p = p.next
    }
    list
}
//
//fun ListNode?.println() = {
//    val lists = toList()
//    lists.println()
//}

fun createNodeList(nodes: String): ListNode? {

    val nodesVal = nodes.trim().split("->").map { it.toInt() }.toIntArray()

    return createNodeList(*nodesVal)

}

fun createNodeList(vararg nodesVal: Int): ListNode? {


    var head: ListNode? = null

    var prev: ListNode? = ListNode(0, null)
    var next: ListNode?

    val nodeSize = nodesVal.size

    for (i in 0 until nodeSize) {

        next = ListNode(nodesVal[i])
        prev?.next = next

        if (i == 0) {
            head = next
        }

        prev = next

    }

    return head
}
